angular.module('Controller')

.controller('QgyCleaningOrderCtrl', ($scope,NgTableParams,menuManage,urls,userData,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            filter:undefined
            enabled:undefined
            status:[]
            serviceTimeStart:undefined
            serviceTimeEnd:undefined
            storeId:$localStorage['accountStore']
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.contractStatusList=commonConstant.contractStatusList(true)
    $scope.appointmentStatusList=commonConstant.appointmentStatusList(true)
    $scope.leaseList=commonConstant.leaseList(true)
    $scope.qgyGoodStatusList=commonConstant.qgyGoodStatusList(true)
    $scope.orderStatuslist=commonConstant.qgyCleaningOrderStatusList()
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )

    #根据传入的动作和要操作的id更新Array
    $scope.updateSelection=($event, id)->
        checkbox = $event.target;
        if checkbox.checked && $scope.search.status.indexOf(id) == -1
            $scope.search.status.push(id)
        if !checkbox.checked && $scope.search.status.indexOf(id) != -1
            idx = $scope.search.status.indexOf(id)
            $scope.search.status.splice(idx, 1)

    #返回true false
    $scope.isSelected=(id)->
        return $scope.search.status.indexOf(id) >= 0

    $scope.doQuery=($defer, params)->
        status = undefined
        if $scope.search.status.length > 0
            status = $scope.search.status.join(",")
        reqData={
            page: params.page() - 1,
            size: params.count(),
            status:status
            storeId:$scope.search.apartmentId
            filter:$scope.search.filter,
        }
        reqData['serviceTimeStart']=moment($scope.search.serviceTimeStart).format('YYYY-MM-DD HH:mm:ss') if $scope.search.serviceTimeStart
        reqData['serviceTimeEnd']=moment($scope.search.serviceTimeEnd).format('YYYY-MM-DD HH:mm:ss') if $scope.search.serviceTimeEnd
        $scope.currentSearchData=$scope.search
        $http.get('/order',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        dealData:(item)->
            theData=angular.copy(item)
            showModal('deal',theData)
        viewData:(item)->
            theData=angular.copy(item)
            theData.type=3
            showDetailModal('view',theData)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        exportData:->
            $scope.exporting = true;
            theData = $scope.currentSearchData;
            status = undefined
            if theData.status.length > 0
                status = theData.status.join(",")
            reqData={
                page: 0,
                size: 999999,
                status:status
                apartmentId:theData.apartmentId
                filter:theData.filter,
            }
            reqData['serviceTimeStart']=moment(theData.serviceTimeStart).format('YYYY-MM-DD') if theData.serviceTimeStart
            reqData['serviceTimeEnd']=moment(theData.serviceTimeEnd).format('YYYY-MM-DD') if theData.serviceTimeEnd
            $http.get('/order/excel',params:reqData)
            .success((resp)->
                $scope.exporting = false
                if resp.code == 0
                    filename = resp.message
                    url=urls.backend+'/order/download?file=' + filename
                    url=url+'&token='+userData.getToken()
                    console.log('url',url)
                    window.open(url, '_blank')
            )
            .error((err)->
                $scope.exporting = false
            )
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyCleaningOrderDealCtrl',
            templateUrl: 'templates/good-manage/cleaning-order/cleaning-order-deal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
    showDetailModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyOrderDetailModalCtrl',
            templateUrl: 'templates/good-manage/cleaning-order/order-detail-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            ''
        )
)